package com.zd.bms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zd.bms.bean.vo.ScenicSpotDetailVo;
import com.zd.bms.service.IScenicSpotService;
import com.zd.bms.service.ITicketInfoService;
import com.zd.bms.service.ITicketOrderService;
import com.zd.common.bean.model.BmsUser;
import com.zd.common.bean.model.TicketInfo;
import com.zd.common.common.utils.LogUtils;

@Controller
@RequestMapping("/selling")
public class SellingTicketsController extends BaseController{
	
	@Autowired
	private IScenicSpotService scenicSpotService;
	
	@Autowired
	private ITicketInfoService ticketInfoService;
	
	@Autowired
	private ITicketOrderService ticketOrderService;
	
	@RequestMapping("/index")
	public ModelAndView index() throws Exception{//取票
		ModelAndView mv = new ModelAndView();
		mv.addObject("user", getSessionUser());
		mv.setViewName("/sellTicket/index");
		mv.addObject("user", getSession("user"));
		return mv;
	}
	
	@RequestMapping("/toVoteTicket")
	public ModelAndView toVoteTicket(Integer scenicSpotId) throws Exception {// 购票
		ModelAndView mv = new ModelAndView();
		ScenicSpotDetailVo scenicSpotDetailVo = scenicSpotService.getScenicSpotDetail(getSessionUser().getScenicSpotId());
		LogUtils.info(scenicSpotId);
		LogUtils.info(scenicSpotDetailVo.getId());
		List<TicketInfo> ticketInfoList = ticketInfoService.getScenicSpotTicketList(scenicSpotId);
		mv.addObject("ticketInfoList", ticketInfoList);
		mv.addObject("user", getSession("user"));
		mv.setViewName("/sellTicket/ticketPurchase");
		return mv;
	}
	
	@RequestMapping("/placeAnOrder")
	@ResponseBody
	public Map<String, Object> placeAnOrder(String userIdInfoList, Integer ticketInfoId) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("result", true);
		BmsUser bmsUser = getSessionUser();
		resultMap.putAll(ticketOrderService.buyTiecktsPC(userIdInfoList, ticketInfoId, bmsUser));
		return resultMap;
	}
	
	@RequestMapping("/activation")
	@ResponseBody
	public Map<String, Object> activation(String userIdInfoList, Integer ticketInfoId) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("result", true);
		BmsUser bmsUser = getSessionUser();
		resultMap.putAll(ticketOrderService.activationTiecktsPC(userIdInfoList, ticketInfoId, bmsUser));
		return resultMap;
	}
	
	@RequestMapping("/binding")
	@ResponseBody
	public Map<String, Object> binding(String userIdInfo, Integer ticketInfoId, Integer orderId ,String checkCode) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("result", true);
		BmsUser bmsUser = getSessionUser();
		resultMap.putAll(ticketOrderService.bindingTiecktsPC(userIdInfo, ticketInfoId, bmsUser, orderId, checkCode));
		return resultMap;
	}
	
}
